Python introduction
Python is a dynamic, interpreted (bytecode-compiled) language.
There are no type declarations of variables, parameters, functions, or methods in source code. This makes the code short and flexible.
Python Source Code
Python source files uses the ``.py" extension and are called ``modules". With a Python module ``hello.py", one way to run it is with the shell command ``python hello.py input_argument".
This shell command ``python hello.py input_argument" calls the Python interpreter to execute the code in ``hello.py", passing it the command line argument ``input_argument".
Fig. Contents in "hello.py" (a illustrative example).
Fig. shows the content in ``hello.py" file, and note that blocks of code are delimited strictly using indentation.
Fig. console response for executing "hello.py" file from the command line.
Fig. shows the response for executing ``hello.py" file from the command line.
Imports, Command-line arguments, and Len()
The statements in module run from top to bottom the first time the module is imported, and they set up variables and functions.
A python module can run directly, or it can be imported and used by other module.
For example, the above ``python hello.py Bob" is a python module run directly.
The special variable ``__ name__" is set to " __main__". Therefore, it is common to have the boilerplate if "__name__==..." shown above to call a main() function when the module is run directly,
In general, "len()" can tell you how long a string is, the number of elements in list and tuples, and the number of key-value pairs in a dictionary.
User-defined functions
Fig. shows a illustrative user-defined function in Python.
Two different ways to repeat strings are presented.
Using the +operator is the first option.
* also means "repeat" operator in Python.
For example, '-' * 10 means '----------'.
From implementation perspective, *works faster than +.
Both + and * operators have different meanings for numbers and for strings.
- and * are called overloaded operator.
The "def" keyword defines the function with its parameters within parentheses and its code indented.
The first line of a function can be a documentation string ("doctoring") that describes what the function does. The doctoring can be a single line, or a multi-line description as in the example above.
Variables defined in the function are local to that function, so the "result" in the above "repeat" function is separate from a "result" variable in another function.
The "return" statement can take an argument, in which case that is the value returned to the caller.
Fig. An illustrative main function to call "repeat" function.
Fig. shows main function which calls the above repeat() function, and print its returns.
At run time, function must be defined by the execution of a "def" before they are called.
Typically, a main() function is defined in the bottom of the file, and other functions called by main function are defined above.
Indentation
White space indentation has meaning in python. Python's use of white space is logical.
A logical block of statements such as the ones that make up a function should have the same indentation.
If one of the lines in a group has a different indentation, it is flagged as a syntax error.
TAB is not recommended as it complicate the indentation scheme.
According to the official Python style guide (PEP 8), indent with 4 spaces is suggested.
Code Checked at Runtime
Python does little checking at compile time, differing almost all type, name, ..., check on each line until that line runs.
Suppose the above main() calls repeat() as following figure,
The If-statement contains an error, where the repeat() function is typed in as repreeeet().
This code compiles and runs fine if the name is not 'Guido'.
When name is `Guido', the codes actually tries to execute the repeeeet().
The interpreter notice that there is no such function and raise an error.
As compared, some other language like verbose type system, like Java, can catch such errors at compile time.
Variable names
Python variables do not have any type spelled out in the source code.
It is suggested to use "name" if it is a single name, and "names" if it is a list of names, and "tuples" if it is a list of tuples.
Some languages prefer underscored_parts for variable names made up of "more than one word", but other languages prefer camelCasing.
Python prefers the underscore method generally.
Keywords like 'print' and 'while' can not be used as variable names.
Built-ins, such as str' and 'list', can not be used as variable names.
Those system variables will be overrided.
Built-ins are not keywords, and are susceptible to inadvertent use by new Python developers.
More on modules and their namespaces
Suppose you've got a module "blinky.py" which contains a "def foo()". The fully qualified name of that foo function is "blinky.foo".
In this way, various Python modules can name their functions and variables, and the variable names won't conflict.
module1.foo is different from module2.foo.
In the python vocabulary, each "blinky", "module1" and "module2" have their "namespaces".
For example, we have the standard "sys" module that contains some standard system facilities, like the arg list, and exit() function. With the statement "import sys", you can then access the definitions in the sys module and make them available by their fully-qualified name, e.g. sys.exit().
"from sys import arg, exit" is another import form.
That makes arg and exit() available by their short names;
However, the original form with the fully-qualified names is recommended, because it is easier to determine where a function or attribute came from.
There are many modules and packages which are bundled with a standard installation of the Python interpreter.
These are collectively known as the "Python Standard Library".
Commonly used "Python Standard Library" include:
sys- access to exit(), arg, stdin, stdout, ...
re - regular expressions.
os - operating system interface, file system
Oline help help(), and dir()
There are various way to get help for Python:
Do a google search, starting with the word "python" like "python list" or "python string lowercase". The first hit is often the answer.
The official Python docs site - docs.python.org- has high quality docs.
There is also an official Tutor mailing list specifically designed for those who are new to python and/or programming.
Many questions can be found on StakOverflow and Quora.
Use the help() and dir() functions.
Inside the Python interpreter, the help() function pulls up documentation strings for various modules, functions, and methods. The dir() function tells you what the attributes of an object are.